//
//  SecondScene.cpp
//  SceneTrans1
//
//  Created by Jaewhan Lee on 12. 12. 8..
//
//

#include "SecondScene.h"
#include "HelloWorldScene.h"
#include "MainPage.h"
#include "SimpleAudioEngine.h"
#include <string>



using namespace cocos2d;
using namespace CocosDenshion;

CCScene* SecondScene::scene()
{
    CCScene *scene = CCScene::create();
    
    SecondScene *layer = SecondScene::create();
    
    scene->addChild(layer);
    
    return scene;
}

bool SecondScene::init()
{
    if ( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) )
    {
        return false;
    }
    
	




	// 초기 판정계수(베인 크기와 비례)
	judge=50;
	// 베인 크기
	vayneSize=0.6;

	// speed
	speed=3.0;
    /////////////////////////////
    

    movingPatton0 = CCMoveTo::create(3.0f, ccp(520,700));
	//move2 = CCMoveTo::create(3.0f, ccp(380,430));
    // 메뉴 아이템 생성 및 초기화
    
	
    
    
	// 게임 뒷 배경
	background = CCSprite::create("Images/gameBack.png");
	background->setPosition(ccp(240, 400));
    //body->setScale(0.5);
	this->addChild(background);

	// making chain
	chain1 = CCSprite::create("Images/blitz/chain1.png");
    chain1->setPosition(ccp(350, 110));
    chain1->setScale(1.5);
    this->addChild(chain1);
	chain2 = CCSprite::create("Images/blitz/chain2.png");
    chain2->setPosition(ccp(350, 110));
    chain2->setScale(1.5);
    this->addChild(chain2);
	chain3 = CCSprite::create("Images/blitz/chain3.png");
    chain3->setPosition(ccp(350, 110));
    chain3->setScale(1.5);
    this->addChild(chain3);
	chain4 = CCSprite::create("Images/blitz/chain4.png");
    chain4->setPosition(ccp(350, 110));
    chain4->setScale(1.5);
    this->addChild(chain4);
	chain5 = CCSprite::create("Images/blitz/chain2.png");
    chain5->setPosition(ccp(350, 110));
    chain5->setScale(1.5);
    this->addChild(chain5);
	chain6 = CCSprite::create("Images/blitz/chain1.png");
    chain6->setPosition(ccp(350, 110));
    chain6->setScale(1.5);
    this->addChild(chain6);
	chain7 = CCSprite::create("Images/blitz/chain3.png");
    chain7->setPosition(ccp(350, 110));
    chain7->setScale(1.5);
    this->addChild(chain7);
	
	
	// making hand
    hand = CCSprite::create("Images/blitz/hand.png");
    hand->setPosition(ccp(330, 100));
    hand->setScale(0.7);
    this->addChild(hand);
	

	// making steam
	steam0 = CCSprite::create("Images/blitz/steam1.png");
    steam0->setPosition(ccp(165, 120));
    steam0->setScale(0.3);
    this->addChild(steam0);
	steam1 = CCSprite::create("Images/blitz/steam2.png");
    steam1->setPosition(ccp(165, 120));
    steam1->setScale(0.3);
    this->addChild(steam1);
	steam2 = CCSprite::create("Images/blitz/steam1.png");
    steam2->setPosition(ccp(165, 120));
    steam2->setScale(0.3);
    this->addChild(steam2);
	
	steam5 = CCSprite::create("Images/blitz/steam2.png");
    steam5->setPosition(ccp(260, 120));
    steam5->setScale(0.3);
    this->addChild(steam5);
	steam6 = CCSprite::create("Images/blitz/steam1.png");
    steam6->setPosition(ccp(260, 120));
    steam6->setScale(0.3);
    this->addChild(steam6);
	steam7 = CCSprite::create("Images/blitz/steam2.png");
    steam7->setPosition(ccp(260, 120));
    steam7->setScale(0.3);
    this->addChild(steam7);

		
	// making head
	head = CCSprite::create("Images/blitz/head.png");
    head->setPosition(ccp(230, 40));
    head->setScale(0.8);
    this->addChild(head);

	// making body
	body = CCSprite::create("Images/blitz/body2.png");
    body->setPosition(ccp(240, 50));
    body->setScale(0.8);
    this->addChild(body);


	// making vayne
	vayne = CCSprite::create("Images/vayne/vayne0.png");
    vayne->setPosition(ccp(-150, 700));
    vayne->setScale(vayneSize);
    this->addChild(vayne);

	animation = CCAnimation::create();
	animation->setDelayPerUnit(0.03333);

	animation->addSpriteFrameWithFileName("Images/vayne/vayne0.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne1.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne2.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne3.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne4.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne5.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne6.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne7.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne8.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne9.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne10.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne11.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne12.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne13.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne14.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne15.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne16.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne17.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne18.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne19.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne20.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne21.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne22.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne23.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne24.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne25.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne26.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne27.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne28.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne29.png");

	animate = CCAnimate::create(animation);
	rep=CCRepeatForever::create(animate);
	vayne->runAction(rep);



	//vayne->setScale(0.4);
	// making vayne2
	/*
	vayne2 = CCSprite::create("vayne.png");
    vayne2->setPosition(ccp(-200, 750));
    vayne2->setScale(0.7);
    this->addChild(vayne2);
	*/
	// baron
	baron = CCSprite::create("Images/baron.png");
	baron->setPosition(ccp(430, 50));
    baron->setScale(0.3);
    this->addChild(baron);

    CCLog("init");

	btnBackground=CCSprite::create("Images/pauseBack.png");
    btnBackground->setPosition(ccp(240, 400));
    this->addChild(btnBackground);
	btnBackground->setVisible(false);
	

	btnPause=CCMenuItemImage::create("Images/btns/btn-pause-normal.png",
											"Images/btns/btn-pause-selected.png",
											this,
											menu_selector(SecondScene::doBtnPause));
	btnContinue=CCMenuItemImage::create("Images/btns/btn-continue-normal.png",
											"Images/btns/btn-continue-selected.png",      
											this,
											menu_selector(SecondScene::doBtnContinue));
	btnReplay=CCMenuItemImage::create("Images/btns/btn-replay-normal.png",
											"Images/btns/btn-replay-selected.png",      
											this,
											menu_selector(SecondScene::doBtnReplay));
	btnBack=CCMenuItemImage::create("Images/btns/btn-back-normal.png",
											"Images/btns/btn-back-selected.png",       
											this,
											menu_selector(SecondScene::doBtnBack));

	btnPause->setPosition(0,0);
	btnContinue->setPosition(75,0);
	btnReplay->setPosition(-75,0);
	btnBack->setPosition(75,0);
	
	btnContinue->setVisible(false);
	btnReplay->setVisible(false);
	btnBack->setVisible(false);

	btnPause->setScale(0.3);
	btnPause->setPosition(190,-370);

	CCMenu* gamePause = CCMenu::create(btnPause,btnContinue,btnReplay,btnBack, NULL);
	//CCMenu* gameOver = CCMenu::create(btnReplay,btnBack, NULL);
	
	//gamePause->setPosition(100,100);

    this->addChild(gamePause);
	//this->addChild(gameOver);


	// 바론(타이머) 초기화
	
	baronY=50.0;
	baronAngle=0.0;
	

	// 처음 베인 무빙 시작~
	vayne->runAction(movingPatton0);


	// 점수
	score=0;
	scoreLabel= CCLabelTTF::create("0","Thonburi",50);
	scoreLabel->setPosition(ccp(73,30));
	this->addChild(scoreLabel);

	level=0;

    return true;
}

void SecondScene::addScore(std::string name, int score){
	std::string str;
	std::string str1;
	char* transstr;
	char* transstr1;
	int i=1;
	char transi[10];
	char transi1[10];
	int upScore,downScore;
	int present_score;
	
	present_score = CCUserDefault::sharedUserDefault()->getIntegerForKey(name.c_str());

	if(present_score >= score){
		return ;
	}
	//CCUserDefault::sharedUserDefault()->setStringForKey("5","hongdro");
	else{
		CCUserDefault::sharedUserDefault()->setIntegerForKey(name.c_str(),score);

		for(;;){
			myitoa(i,transi);
			myitoa(i+1,transi1);
			str = CCUserDefault::sharedUserDefault()->getStringForKey(transi);
			str1 = CCUserDefault::sharedUserDefault()->getStringForKey(transi1);
			transstr = (char*)str.c_str();
			transstr1 = (char*)str1.c_str();
			upScore = CCUserDefault::sharedUserDefault()->getIntegerForKey(transstr);
			downScore = CCUserDefault::sharedUserDefault()->getIntegerForKey(transstr1);	
		
			if(str==""){
				CCUserDefault::sharedUserDefault()->setStringForKey(transi,name);
				break;
			}
		
			if(str1==""){
				if(score>upScore){
					CCUserDefault::sharedUserDefault()->setStringForKey(transi,name);
					CCUserDefault::sharedUserDefault()->setStringForKey(transi1,str1);
					break;
				}
				else{
					CCUserDefault::sharedUserDefault()->setStringForKey(transi1,name);
					break;
				}
			}
		
			if(score<=upScore && score > downScore){
				for(;;){
					i++;
					myitoa(i,transi);
					str = CCUserDefault::sharedUserDefault()->getStringForKey(transi);
					CCUserDefault::sharedUserDefault()->setStringForKey(transi,name);
					name=str;
					myitoa(i+1,transi1);
					str1 = CCUserDefault::sharedUserDefault()->getStringForKey(transi1);
				
					if(str1==""){
						CCUserDefault::sharedUserDefault()->setStringForKey(transi1,str);
						break;
					}
				
				}
				CCUserDefault::sharedUserDefault()->flush();
				break;
			}
			i++;
	
		}
	}
	
}




void SecondScene::baronAni(float temp){
	//static int x=430;
	
	

	

	if(((int)(baronY-50)/10)%2==0) baronAngle+=0.5;
	else baronAngle-=0.5;

	baron->setRotation(baronAngle);
	
	baron->setPositionY(baronY);

	if(baronY<=500) baronY+=0.25;

	char timeString[5]={0};
	int timeInt=(int)((baronY-50)/7.5);
	myitoa(timeInt,timeString);
	//CCLog(temp2);

	if(timeInt==60){
		// 시간 60초되면 게임 끗~
		CCLog("game over");

		btnReplay->setVisible(true);
		btnBack->setVisible(true);

		btnBackground->setVisible(true);

		if(level==0){
			vayne->stopAction(movingPatton0);
		}
		else if(level==1){
			vayne->stopAction(movingPatton1);
		}
		else if(level==2){
			vayne->stopAction(movingPatton2);
		}
		else if(level==3){
			vayne->stopAction(movingPatton3);
		}
		
		else {
			vayne->stopAction(movingPatton0);
		}

		vayne->stopAction(rep);

		this->unschedule(schedule_selector(SecondScene::collisionCheck));
	

		this->unschedule(schedule_selector(SecondScene::blitzBodyAni));
		this->unschedule(schedule_selector(SecondScene::blitzHeadAni));
		this->unschedule(schedule_selector(SecondScene::blitzSteamAni));
		this->unschedule(schedule_selector(SecondScene::baronAni));


		
		//vayne->runAction(movingPatton0);

		
	}


	//head->setScaleY(plus);

}

void SecondScene::blitzHeadAni(float temp){
	static int plus=10;
	if(plus%12==0) head->setPosition(ccp(230,40+1));
	else if(plus%12==1) head->setPosition(ccp(230,40+2));
	else if(plus%12==2) head->setPosition(ccp(230-1,40+3));
	else if(plus%12==3) head->setPosition(ccp(230-2,40+2));
	else if(plus%12==4) head->setPosition(ccp(230-2,40+1));
	else if(plus%12==5) head->setPosition(ccp(230-2,40+0));
	else if(plus%12==6) head->setPosition(ccp(230-2,40+1));
	else if(plus%12==7) head->setPosition(ccp(230-2,40+2));
	else if(plus%12==8) head->setPosition(ccp(230-1,40+3));
	else if(plus%12==9) head->setPosition(ccp(230,40+2));
	else if(plus%12==10) head->setPosition(ccp(230,40+1));
	else if(plus%12==11) head->setPosition(ccp(230,40));

	plus+=1;
	if(plus>11) plus=0;

	//head->setScaleY(plus);

}

void SecondScene::blitzBodyAni(float temp){
	static int plus=0;
	if(plus%12==0) body->setPosition(ccp(240,50+1));
	else if(plus%12==1) body->setPosition(ccp(240,50+2));
	else if(plus%12==2) body->setPosition(ccp(240-1,50+3));
	else if(plus%12==3) body->setPosition(ccp(240-2,50+2));
	else if(plus%12==4) body->setPosition(ccp(240-2,50+1));
	else if(plus%12==5) body->setPosition(ccp(240-2,50+0));
	else if(plus%12==6) body->setPosition(ccp(240-2,50+1));
	else if(plus%12==7) body->setPosition(ccp(240-2,50+2));
	else if(plus%12==8) body->setPosition(ccp(240-1,50+3));
	else if(plus%12==9) body->setPosition(ccp(240,50+2));
	else if(plus%12==10) body->setPosition(ccp(240,50+1));
	else if(plus%12==11) body->setPosition(ccp(240,50));

	plus+=1;
	if(plus>11) plus=0;

	// 점수 감소ㅋ
	if(score>0)
		score-=1;

}
void SecondScene::blitzSteamAni(float temp){
	/*static int plus=0;
	if(plus%12==0) body->setPosition(ccp(240,50+1));
	else if(plus%12==1) body->setPosition(ccp(240,50+2));
	else if(plus%12==2) body->setPosition(ccp(240-1,50+3));
	else if(plus%12==3) body->setPosition(ccp(240-2,50+2));
	else if(plus%12==4) body->setPosition(ccp(240-2,50+1));
	else if(plus%12==5) body->setPosition(ccp(240-2,50+0));
	else if(plus%12==6) body->setPosition(ccp(240-2,50+1));
	else if(plus%12==7) body->setPosition(ccp(240-2,50+2));
	else if(plus%12==8) body->setPosition(ccp(240-1,50+3));
	else if(plus%12==9) body->setPosition(ccp(240,50+2));
	else if(plus%12==10) body->setPosition(ccp(240,50+1));
	else if(plus%12==11) body->setPosition(ccp(240,50));

	plus+=1;
	if(plus>11) plus=0;
	*/

	
	CCActionInterval *steamUp = CCMoveBy::create(2.0f,ccp(-2,151));
	CCActionInterval *steamUpSlowly = CCEaseExponentialOut::create(steamUp);
	CCActionInterval *steamBack = CCMoveBy::create(0.000000000001f,ccp(2,-151));

	CCActionInterval *steamUp1 = CCMoveBy::create(2.0f,ccp(2,149));
	CCActionInterval *steamUpSlowly1 = CCEaseExponentialOut::create(steamUp1);
	CCActionInterval *steamBack1 = CCMoveBy::create(0.000000000001f,ccp(-2,-149));

	CCActionInterval *steamUp2 = CCMoveBy::create(2.0f,ccp(0,150));
	CCActionInterval *steamUpSlowly2 = CCEaseExponentialOut::create(steamUp2);
	CCActionInterval *steamBack2 = CCMoveBy::create(0.000000000001f,ccp(0,-150));

	CCActionInterval *steamUp3 = CCMoveBy::create(2.0f,ccp(-2,156));
	CCActionInterval *steamUpSlowly3 = CCEaseExponentialOut::create(steamUp3);
	CCActionInterval *steamBack3 = CCMoveBy::create(0.000000000001f,ccp(2,-156));

	CCActionInterval *steamUp4 = CCMoveBy::create(2.0f,ccp(2,154));
	CCActionInterval *steamUpSlowly4 = CCEaseExponentialOut::create(steamUp4);
	CCActionInterval *steamBack4 = CCMoveBy::create(0.000000000001f,ccp(-2,-154));

	CCActionInterval *steamUp5 = CCMoveBy::create(2.0f,ccp(0,154));
	CCActionInterval *steamUpSlowly5 = CCEaseExponentialOut::create(steamUp5);
	CCActionInterval *steamBack5 = CCMoveBy::create(0.000000000001f,ccp(0,-154));




	CCFiniteTimeAction *steamAction0=CCSequence::create(steamUpSlowly,CCDelayTime::create(0.4),steamBack, NULL);
	
	CCFiniteTimeAction *steamAction1=CCSequence::create(CCDelayTime::create(0.2),steamUpSlowly1,CCDelayTime::create(0.2),steamBack1, NULL);

	CCFiniteTimeAction *steamAction2=CCSequence::create(CCDelayTime::create(0.4),steamUpSlowly2,steamBack2, NULL);

	CCFiniteTimeAction *steamAction3=CCSequence::create(steamUpSlowly3,CCDelayTime::create(0.4),steamBack3, NULL);
	
	CCFiniteTimeAction *steamAction4=CCSequence::create(CCDelayTime::create(0.2),steamUpSlowly4,CCDelayTime::create(0.2),steamBack4, NULL);

	CCFiniteTimeAction *steamAction5=CCSequence::create(CCDelayTime::create(0.4),steamUpSlowly5,steamBack5, NULL);
	
	

	steam0->runAction(steamAction0);
	
	steam1->runAction(steamAction1);

	steam2->runAction(steamAction2);
	
	steam5->runAction(steamAction3);
	
	steam6->runAction(steamAction4);
	
	steam7->runAction(steamAction5);
	



	
	
	//CCDelayTime::create(3.2);
	//steam1->setPosition(ccp(100,100));
}

int SecondScene::myitoa(int nNum, char* szBuffer)
{
	int i, nCount, nBuffer;
	nCount = nNum;
	nBuffer = 0;
	while(true)
	{
		if(nCount != 0)
		{
			nBuffer++;
			nCount = nCount/ 10;
		}
		else
		{
			break;
		}
	}
	szBuffer[nBuffer] = NULL; 
	for(i = nBuffer - 1; i >= 0; i--)
	{
		nCount = nNum % 10;
		szBuffer[i] = nCount + '0';
		nNum = nNum / 10;
	}
	return nBuffer + 1;
}

void SecondScene::collisionCheck(float temp){
	//정지버튼 활성화
	if(hand->getPositionY()>=101.0){
		btnPause->setEnabled(false);
		grab->setVisible(false);
		
	}

	if(hand->getPositionY()<100.5){
		btnPause->setEnabled(true);
		grab->setVisible(true);
		
	}


	
	// 충돌범위
	if(abs(vayne->getPositionX() - hand->getPositionX()) < judge && abs(vayne->getPositionY() - hand->getPositionY()) < judge){
		
		
		collision=1;
		
		//충돌 후 베인의 액션 정지
		if(level==0){
			vayne->stopAction(movingPatton0);
		}
		else if(level==1){
			vayne->stopAction(movingPatton1);
		}
		else if(level==2){
			vayne->stopAction(movingPatton2);
		}
		else if(level==3){
			vayne->stopAction(movingPatton3);
		}
		
		else {
			vayne->stopAction(movingPatton0);
		}
	}

	// 충돌(베인 잡혔을 때)
	if(collision==1){

		// 베인 딸려옴
		vayne->setPositionX(hand->getPositionX());
		vayne->setPositionY(hand->getPositionY());
		
		// 다 딸려오면 위치 초기화, 점수추가, 패턴변화
		if(vayne->getPositionY()<250){
			score=score+(300*(level+1));
			//score=99999;

			// 베인 스피드 제한
			if(speed>1.5) speed=speed-0.2;
			else if(speed<=1.5 && speed >= 0.5) speed=speed-0.1;
			else if(speed<0.5) speed=0.5;

			// 기본 난이도 조정
			judge=50-((3.0-speed)*1.2);
			vayneSize=(0.9-((3.0-speed)*0.16))-0.3;

			
			// 베인 크기 축소
			vayne->setScale(vayneSize);
			

			// 베인 위치초기화
			vayne->setPositionX(-150.0);
			vayne->setPositionY(700.0);
			collision=0;

			// 액션 변경
			if(level==0){
				level=1;
				CCActionInterval* patton1 = CCMoveTo::create(2.5f, ccp(150,700)); // 180,750
				CCActionInterval *steamUpSlowly = CCEaseExponentialOut::create(patton1);
				CCActionInterval* patton2 = CCMoveTo::create(0.6f, ccp(520,700));
				
				movingPatton1=CCSequence::create(steamUpSlowly,patton2, NULL);
				//CCActionInterval *move = CCMoveBy::create(4.0f,ccp(200,200));
				//CCActionInterval *move_ease_in = CCEaseIn::create(move,4.0f);

				//movingPatton0 = CCMoveTo::create(speed, ccp(520,750));


				vayne->runAction(movingPatton1);
			}
			else if(level==1){
				level=2;
				CCActionInterval* patton1 = CCMoveTo::create(2.0f, ccp(170,700));
				CCActionInterval *steamUpSlowly1 = CCEaseExponentialOut::create(patton1);

				CCActionInterval* patton2 = CCMoveTo::create(1.5f, ccp(50,700));
				CCActionInterval *steamUpSlowly2 = CCEaseExponentialInOut::create(patton2);

				CCActionInterval* patton3 = CCMoveTo::create(0.6f, ccp(520,700));

				movingPatton2=CCSequence::create(steamUpSlowly1,steamUpSlowly2,patton3, NULL);
				//CCActionInterval *move = CCMoveBy::create(4.0f,ccp(200,200));
				//CCActionInterval *move_ease_in = CCEaseIn::create(move,4.0f);

				//movingPatton0 = CCMoveTo::create(speed, ccp(520,750));
				vayne->runAction(movingPatton2);
			}
			else if(level==2){
				level=3;
				CCActionInterval* patton1 = CCMoveTo::create(2.0f, ccp(170,700));
				CCActionInterval *steamUpSlowly1 = CCEaseExponentialOut::create(patton1);

				CCActionInterval* patton2 = CCMoveTo::create(1.5f, ccp(170,1050));
				CCActionInterval *steamUpSlowly2 = CCEaseExponentialIn::create(patton2);

				CCActionInterval* patton3 = CCMoveTo::create(2.0f, ccp(170,700));
				CCActionInterval *steamUpSlowly3 = CCEaseExponentialOut::create(patton3);

				CCActionInterval* patton4 = CCMoveTo::create(0.6f, ccp(520,700));

				movingPatton3=CCSequence::create(steamUpSlowly1,steamUpSlowly2,steamUpSlowly3,patton4, NULL);
				//CCActionInterval *move = CCMoveBy::create(4.0f,ccp(200,200));
				//CCActionInterval *move_ease_in = CCEaseIn::create(move,4.0f);

				//movingPatton0 = CCMoveTo::create(speed, ccp(520,750));
				vayne->runAction(movingPatton3);
			}
			else if(level==3){
				level=0;
				vayne->setPositionX(-150.0);
				vayne->setPositionY(700.0);
				movingPatton0 = CCMoveTo::create(speed, ccp(520,700));
				vayne->runAction(movingPatton0);
				level=0;
			}
			
		}
	}

	// 걍 지나갔을 때
	else if(vayne->getPositionX()==520.0){
		vayne->setPositionX(-150.0);
		vayne->setPositionY(700.0);
		movingPatton0 = CCMoveTo::create(speed, ccp(520,700));
		vayne->runAction(movingPatton0);
		level=0;

	}

	// 점수 증가
	static char scoreTemp[20];

	//scoreTemp=score-48;

	//scoreLabel = CCMenuItemFont::create(&scoreTemp);
	//score->setColor(ccc3(0, 0, 0));
	//scoreLabel->setScale(2.0);
	//score->setFontSize(300);
	//scoreLabel->setPosition(-100,0);

	myitoa(score,scoreTemp);

	if(score==0) scoreLabel->setString("0");
	else scoreLabel->setString(scoreTemp);


	


	//CCLog("%f %f",vayne->getPositionX(),hand->getPositionX());
}

void SecondScene::onEnter()
{
    CCLayer::onEnter();

    CCLog("onEnter");
}

void SecondScene::doBtnPause(CCObject* pSender){
	btnBackground->setVisible(true);
	grab->setVisible(false);
	btnContinue->setVisible(true);
	btnReplay->setVisible(true);



	
	if(level==0){
			vayne->stopAction(movingPatton0);
		}
		else if(level==1){
			vayne->stopAction(movingPatton1);
		}
		else if(level==2){
			vayne->stopAction(movingPatton2);
		}
		else if(level==3){
			vayne->stopAction(movingPatton3);
		}
		
		else {
			vayne->stopAction(movingPatton0);
		}

	vayne->stopAction(rep);

	this->unschedule(schedule_selector(SecondScene::collisionCheck));
	

	this->unschedule(schedule_selector(SecondScene::blitzBodyAni));
	this->unschedule(schedule_selector(SecondScene::blitzHeadAni));
	this->unschedule(schedule_selector(SecondScene::blitzSteamAni));
	this->unschedule(schedule_selector(SecondScene::baronAni));

}

void SecondScene::doBtnContinue(CCObject* pSender){
	btnBackground->setVisible(false);

	animation = CCAnimation::create();
	animation->setDelayPerUnit(0.03333);

	animation->addSpriteFrameWithFileName("Images/vayne/vayne0.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne1.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne2.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne3.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne4.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne5.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne6.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne7.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne8.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne9.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne10.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne11.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne12.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne13.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne14.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne15.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne16.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne17.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne18.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne19.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne20.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne21.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne22.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne23.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne24.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne25.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne26.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne27.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne28.png");
	animation->addSpriteFrameWithFileName("Images/vayne/vayne29.png");

	animate = CCAnimate::create(animation);
	rep=CCRepeatForever::create(animate);
	vayne->runAction(rep);


	if(level==1){
			CCActionInterval* patton1 = CCMoveTo::create(2.5f, ccp(150,700)); // 180,750
			CCActionInterval *steamUpSlowly = CCEaseExponentialOut::create(patton1);
			CCActionInterval* patton2 = CCMoveTo::create(0.6f, ccp(520,700));
				
			movingPatton1=CCSequence::create(steamUpSlowly,patton2, NULL);
			//CCActionInterval *move = CCMoveBy::create(4.0f,ccp(200,200));
			//CCActionInterval *move_ease_in = CCEaseIn::create(move,4.0f);

			//movingPatton0 = CCMoveTo::create(speed, ccp(520,750));


			vayne->runAction(movingPatton1);
		}
		else if(level==2){
			
			CCActionInterval* patton1 = CCMoveTo::create(2.0f, ccp(170,700));
			CCActionInterval *steamUpSlowly1 = CCEaseExponentialOut::create(patton1);

			CCActionInterval* patton2 = CCMoveTo::create(1.5f, ccp(50,700));
			CCActionInterval *steamUpSlowly2 = CCEaseExponentialInOut::create(patton2);

			CCActionInterval* patton3 = CCMoveTo::create(0.6f, ccp(520,700));

			movingPatton2=CCSequence::create(steamUpSlowly1,steamUpSlowly2,patton3, NULL);
			//CCActionInterval *move = CCMoveBy::create(4.0f,ccp(200,200));
			//CCActionInterval *move_ease_in = CCEaseIn::create(move,4.0f);

			//movingPatton0 = CCMoveTo::create(speed, ccp(520,750));
			vayne->runAction(movingPatton2);
		}
		else if(level==3){

			CCActionInterval* patton1 = CCMoveTo::create(2.0f, ccp(170,700));
			CCActionInterval *steamUpSlowly1 = CCEaseExponentialOut::create(patton1);

			CCActionInterval* patton2 = CCMoveTo::create(1.5f, ccp(170,1050));
			CCActionInterval *steamUpSlowly2 = CCEaseExponentialIn::create(patton2);

			CCActionInterval* patton3 = CCMoveTo::create(2.0f, ccp(170,700));
			CCActionInterval *steamUpSlowly3 = CCEaseExponentialOut::create(patton3);

			CCActionInterval* patton4 = CCMoveTo::create(0.6f, ccp(520,700));

			movingPatton3=CCSequence::create(steamUpSlowly1,steamUpSlowly2,steamUpSlowly3,patton4, NULL);
			//CCActionInterval *move = CCMoveBy::create(4.0f,ccp(200,200));
			//CCActionInterval *move_ease_in = CCEaseIn::create(move,4.0f);

			//movingPatton0 = CCMoveTo::create(speed, ccp(520,750));
			vayne->runAction(movingPatton3);
		}
		else{
			level=0;
			
			movingPatton0 = CCMoveTo::create(speed, ccp(520,700));
			vayne->runAction(movingPatton0);
			
		}


	this->schedule(schedule_selector(SecondScene::collisionCheck));
	

	this->schedule(schedule_selector(SecondScene::blitzBodyAni),0.05f);
	this->schedule(schedule_selector(SecondScene::blitzHeadAni),0.05f);
	this->schedule(schedule_selector(SecondScene::blitzSteamAni),5.0f);
	this->schedule(schedule_selector(SecondScene::baronAni),0.0333f);



	btnContinue->setVisible(false);
	btnReplay->setVisible(false);

}
void SecondScene::doBtnReplay(CCObject* pSender){
	init();
	

	this->schedule(schedule_selector(SecondScene::collisionCheck));
	

	this->schedule(schedule_selector(SecondScene::blitzBodyAni),0.05f);
	this->schedule(schedule_selector(SecondScene::blitzHeadAni),0.05f);
	this->schedule(schedule_selector(SecondScene::blitzSteamAni),5.0f);
	this->schedule(schedule_selector(SecondScene::baronAni),0.0333f);

	btnContinue->setVisible(false);
	btnReplay->setVisible(false);
}
void SecondScene::doBtnBack(CCObject* pSender){
	CCScene* pScene = MainPage::scene();
//    CCDirector::sharedDirector()->pushScene( pScene );
//    CCDirector::sharedDirector()->pushScene( CCTransitionProgressRadialCCW::create(1, pScene) );
    CCDirector::sharedDirector()->replaceScene( pScene );
}

void SecondScene::onEnterTransitionDidFinish()
{
	// 버튼(블리츠 몸통임ㅋ)    "      \n      \n      \n      " "######\n######\n######\n######"    
	grab = CCMenuItemFont::create("      \n      \n      \n      ",
                                                   this,
                                                   menu_selector(SecondScene::doGrab)
                                                   );
    grab->setColor(ccc3(0, 0, 0));
	grab->setPosition(ccp(70,20));
	grab->setFontSize(10);
	
	

    // 메뉴 생성
    CCMenu* pMenu = CCMenu::create( grab, NULL );
    
    // 메뉴 위치
    pMenu->setPosition(140, 40);

//	pMenu->setScale(5.0);
    
    // 레이어에 메뉴 객체 추가
    this->addChild(pMenu);


	this->schedule(schedule_selector(SecondScene::collisionCheck));
	

	this->schedule(schedule_selector(SecondScene::blitzBodyAni),0.05f);
	this->schedule(schedule_selector(SecondScene::blitzHeadAni),0.05f);
	this->schedule(schedule_selector(SecondScene::blitzSteamAni),5.0f);
	this->schedule(schedule_selector(SecondScene::baronAni),0.0333f);

	
	
	
	
	//vayne2->runAction(move2);

	
	
	


    CCLayer::onEnterTransitionDidFinish();

    CCLog("onEnterTransitionDidFinish");
}

void SecondScene::onExitTransitionDidStart()
{
    CCLayer::onExitTransitionDidStart();
    
    CCLog("onExitTransitionDidStart");
}

void SecondScene::onExit()
{
    CCLayer::onExit();
    
    CCLog("onExit");
}

SecondScene::~SecondScene()
{
    CCLog("dealloc");
}

void SecondScene::doGrab(CCObject* pSender)
{
	// 발사 정지
	grab->setVisible(false);

	// 정지버튼 비활성화..(주먹 나가는 동안에 꺼둠)
	btnPause->setEnabled(false);

	// chain
	CCActionInterval* chMoving1 = CCMoveTo::create(0.4f, ccp(260,700));
	CCActionInterval* chMoving11 = CCMoveTo::create(0.4f, ccp(350, 110));
	CCActionInterval* chMoving2 = CCMoveTo::create(0.4f, ccp(276,630));
	CCActionInterval* chMoving22 = CCMoveTo::create(0.4f, ccp(350, 110));
	CCActionInterval* chMoving3 = CCMoveTo::create(0.4f, ccp(283,545));
	CCActionInterval* chMoving33 = CCMoveTo::create(0.4f, ccp(350, 110));
	CCActionInterval* chMoving4 = CCMoveTo::create(0.4f, ccp(295,445));
	CCActionInterval* chMoving44 = CCMoveTo::create(0.4f, ccp(350, 110));
	CCActionInterval* chMoving5 = CCMoveTo::create(0.4f, ccp(315,340));
	CCActionInterval* chMoving55 = CCMoveTo::create(0.4f, ccp(350, 110));
	CCActionInterval* chMoving6 = CCMoveTo::create(0.4f, ccp(330,263));
	CCActionInterval* chMoving66 = CCMoveTo::create(0.4f, ccp(350, 110));
	CCActionInterval* chMoving7 = CCMoveTo::create(0.4f, ccp(340,185));
	CCActionInterval* chMoving77 = CCMoveTo::create(0.4f, ccp(350, 110));

//	CCFiniteTimeAction* chainSeq=CCSpawn::create(chMoving1,chMoving2,chMoving3,chMoving4,chMoving5,chMoving6,chMoving7, NULL);

	CCFiniteTimeAction* chainSeq1=CCSequence::create(chMoving1,chMoving11, NULL);
	CCFiniteTimeAction* chainSeq2=CCSequence::create(chMoving2,chMoving22, NULL);
	CCFiniteTimeAction* chainSeq3=CCSequence::create(chMoving3,chMoving33, NULL);
	CCFiniteTimeAction* chainSeq4=CCSequence::create(chMoving4,chMoving44, NULL);
	CCFiniteTimeAction* chainSeq5=CCSequence::create(chMoving5,chMoving55, NULL);
	CCFiniteTimeAction* chainSeq6=CCSequence::create(chMoving6,chMoving66, NULL);
	CCFiniteTimeAction* chainSeq7=CCSequence::create(chMoving7,chMoving77, NULL);


	chain1->runAction(chainSeq1);
	chain2->runAction(chainSeq2);
	chain3->runAction(chainSeq3);
	chain4->runAction(chainSeq4);
	chain5->runAction(chainSeq5);
	chain6->runAction(chainSeq6);
	chain7->runAction(chainSeq7);
	
	// hand
	CCActionInterval* move1 = CCMoveTo::create(0.4f, ccp(240,780));
	CCActionInterval* move2 = CCMoveTo::create(0.4f, ccp(330,100));

	CCFiniteTimeAction* seq=CCSequence::create(move1,move2, NULL);

	hand->runAction(seq);


	// button
	CCActionInterval* move3 = CCMoveTo::create(0.1f, ccp(70,-1000));
	CCActionInterval* move4 = CCMoveTo::create(0.7f, ccp(70,20));
	CCFiniteTimeAction* seq2=CCSequence::create(move3,move4, NULL);

	grab->runAction(seq2);

	SimpleAudioEngine::sharedEngine()->playEffect("sounds/shoot.wav");


////    CCDirector::sharedDirector()->popScene();
//
//    CCScene* pScene = HelloWorld::scene();
//    CCDirector::sharedDirector()->replaceScene( pScene );
////    CCDirector::sharedDirector()->replaceScene( CCTransitionProgressRadialCCW::create(1, pScene) );
//
}







